package com.wedo.mall.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.e.library.activity.EActivity;
import com.e.library.entity.User;
import com.e.library.http.EApi;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.utils.ESPUtils;
import com.wedo.mall.R;
import com.wedo.mall.payment.Payment;
import com.wedo.mall.util.WUtils;
import com.wedo.mall.widget.PaymentView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;

import static com.wedo.mall.activity.login.WLogin.USER;

/**
 * 充值
 */
public class MyWalletRechangeActivity extends EActivity {

    @BindView(R.id.btn_recharge) Button btn_recharge;
    @BindView(R.id.et_recharge) EditText et_recharge;
    @BindView(R.id.tv_recharge) TextView tv_recharge;
    @BindView(R.id.pv_payment) PaymentView pv_payment;

    private Payment payment;

    @Override
    protected int layout() {
        return R.layout.activity_my_wallet_rechange;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        btn_recharge.setEnabled(false);
        pv_payment.recharge();
        payment = Payment.with(this).listener(new Payment.OnPayListener() {
            @Override
            public void successful() {
                /**充值成功，调用接口更新用户账号金额，更新我的钱包金额*/
                String recharge = et_recharge.getText().toString().trim();

                toast(getString(R.string.user_recharge_successfully));

                commitOrder(recharge);
                //
            }
        });
    }

    /**
     * 充值下单接口
     */
    private void commitOrder(final String money){
        loading();
        Map<String,String> params = new HashMap<>();
        params.put("money",money);
        post(EApi.USER_COMMITORDER, params, new EObserver() {
            @Override
            public void successful(EResponse response) {
                String order_sn = response.string("order_sn");
                rechargeMoney(order_sn,money);
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });
    }

    /**
     * 充值成功接口
     */
    private void rechargeMoney(String order_sn,String money){
        Map<String,String> params = new HashMap<>();
        params.put("money",money);
        params.put("order_sn",order_sn);
        post(EApi.USER_RECHARGEMONEY, params, new EObserver() {
            @Override
            public void successful(EResponse response) {
                getUserInfo();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });
    }

    /**
     * 获取个人信息
     */
    private void getUserInfo(){
        loading();
        EClient.with().post(EApi.USER_INFO, new EObserver() {
            @Override
            public void successful(EResponse response) {
                //List<User> user = response.list(User.class);
                User user = response.object("list",User.class);
                //保存USER
                ESPUtils.setString(MyWalletRechangeActivity.this,USER,user.toString());
                dismiss();
                defaultFinish();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }

        });
    }

    @OnTextChanged(R.id.et_recharge)
    protected void onRechargeChange(Editable s){
        String str = s.toString().trim();
        if (TextUtils.isEmpty(str)){
            tv_recharge.setText("￥0.00");
            btn_recharge.setEnabled(false);
            return;
        }

        btn_recharge.setEnabled(true);
        if (TextUtils.equals(".",str)){
            tv_recharge.setText("￥0.00");
        } else if (str.startsWith(".")){
            tv_recharge.setText(String.format("￥0%s",str));
        }else if (str.endsWith(".")){
            tv_recharge.setText(String.format("￥%s0",str));
        }else {
            tv_recharge.setText(String.format("￥%s",str));
        }
    }

    @OnClick(R.id.btn_recharge)
    protected void onRechargeClick(){
        String recharge = et_recharge.getText().toString().trim();
        if (TextUtils.isEmpty(recharge)){
            return;
        }

        if (TextUtils.equals(".",recharge)){
            return;
        }

        double d = WUtils.toDouble(recharge);
        if (d < 100){
            toast(getString(R.string.user_minimumtopup));
            return;
        }

        payment.amount(recharge);
        payment.onPaySelected(pv_payment);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        payment.onActivityResult(requestCode,resultCode,data);
    }
}
